package training.bms.business;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import training.framework.business.AbstractEntity;
import training.framework.business.BusinessException;


@Entity
@Table(name="POS_POST")
public class Post  extends AbstractEntity<Integer> {
	private Integer identifier;
	private String title;
	private String content;
	private Date creationDate;
	private Blog blog;
	
	@Override
	public void setIdentifier(Integer identifier) {
		this.identifier = identifier;
	}

	@Override
	@Id
	@Column(name="POS_IDENTIFIER")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getIdentifier() {
		return identifier;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	@Column(name="POS_TITLE")
	@NotNull
	@Size(min=1, max=100, message="Please specify the title of this post (100 characters maximum)")
	public String getTitle() {
		return title;
	}

	public void setContent(String content) {
		this.content = content;
	}	

	@Column(name="POS_CONTENT")
	@NotNull
	@Size(min=1, max=10000, message="Please specify the content of this post (10000 characters maximum)")		
	public String getContent() {
		return content;
	}
	
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	
	@NotNull
	@Column(name="POST_CREATION_DATE")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreationDate() {
		return creationDate;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}
	
	@NotNull
	@ManyToOne
	@JoinColumn(name="BLO_IDENTIFIER")
	public Blog getBlog() {
		return blog;
	}	
	
	// ------------------- RELATION ------------------- //
	
	private List<Tag> jpaTags = new ArrayList<Tag>();;

	@SuppressWarnings("unused")
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(
		joinColumns=@JoinColumn(name="POS_IDENTIFIER"),	
		inverseJoinColumns=@JoinColumn(name="TAG_IDENTIFIER"),
		uniqueConstraints=@UniqueConstraint(columnNames={"POS_IDENTIFIER", "TAG_IDENTIFIER"})
	)
	private List<Tag> getJpaTags() {
		return jpaTags;
	}

	@SuppressWarnings("unused")
	private void setJpaTags(List<Tag> jpaTags) {
		this.jpaTags = jpaTags;
	}
	
	@Transient
	public List<Tag> getTags(){
		return Collections.unmodifiableList(jpaTags); 
	}
	
	public void clearTags(){
		jpaTags.clear();
	}
	
	public void addTag(Tag tag){
		for(Tag jpaTag : jpaTags){
			if(jpaTag.getIdentifier() == tag.getIdentifier()){
				throw new BusinessException("Post already associated to tag " + tag.getName());
			}
		}
		jpaTags.add(tag);
	}
}
